// pages/coupon/coupon.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        price: '',
        time: '',
        userInfo: {}
    },
    getQueryString: function(url, name) {
        var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
        var r = url.substr(1).match(reg)
        if (r != null) {
            return r[2]
        }
        return null
    },
    getCoupon: function(options) {
        if (options.q) {
            var q = decodeURIComponent(options.q)
            console.log("index 生命周期 onload url=" + q)
            console.log("index 生命周期 onload 参数 id=" + this.getQueryString(q, 'id'))
            var id = this.getQueryString(q, 'id')
            console.log(id)
            this.setData({
                id: id
            })
            this.getDetail()
        }
    },
    getDetail: function() {
        var _this = this
        wx.request({
            url: app.globalData.url+'/v1/payment/activi',
            method: 'GET',
            data: {
                openid: app.globalData.openId,
                code: _this.data.id
            },
            success: function(r) {
                r = r.data
                if (r.code == 0) {
                    _this.setData({
                        price: r.data.price,
                        time: r.data.end_time.substring(0, 10)
                    })
                    wx.hideToast()
                } else {
                    wx.showToast({
                        title: r.data.message,
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        })
    },
    getUser: function(r) {
        var _this = this
        if (r.detail.errMsg == 'getUserInfo:ok') {
            app.globalData.userInfo = r.detail.userInfo
            this.setData({
                userInfo: r.detail.userInfo
            })
            wx.request({
                url: app.globalData.url+'/v1/wechat/user',
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                    nikname: r.detail.userInfo.nickName,
                    headimgurl: r.detail.userInfo.avatarUrl,
                    openid: app.globalData.openId
                },
                success: function(res) {
                    if (res.data.code == 0) {
                        app.globalData.userInfo = res.userInfo
                        _this.ticketRecharge()
                    } else {
                        wx.showToast({
                            title: '获取信息失败',
                            icon: 'none',
                            duration: 2000
                        })
                    }
                }
            })
        }
    },
    ticketRecharge: function() {
        var _this = this
        wx.showToast({
            title: 'loading...',
            icon: 'loading',
            duration: 60000
        })
        if (_this.data.id.substring(0, 1) >= 0 && _this.data.id.substring(0, 1) <= 9) {
            console.log('扫码随机金额-------------')
            wx.request({
                url: app.globalData.url+'/v1/payment/pcode',
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                    code: _this.data.id,
                    openid: app.globalData.openId
                },
                success: function(r) {
                    r = r.data
                    if (r.code == 0) {
                        wx.hideToast()
                        wx.showToast({
                            title: '领取成功！'
                        })
                        setTimeout(function() {
                            wx.reLaunch({
                                url: '/pages/index/index',
                            })
                        }, 1500)
                    } else {
                        wx.hideToast()
                        wx.showToast({
                            title: r.data.message,
                            icon: 'none'
                        })
                    }
                }
            })
        } else {
            console.log('扫码固定金额-------------')
            wx.request({
                url: app.globalData.url+'/v1/payment/code',
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                    code: _this.data.id,
                    openid: app.globalData.openId
                },
                success: function(r) {
                    r = r.data
                    if (r.code == 0) {
                        wx.hideToast()
                        wx.showToast({
                            title: '领取成功！'
                        })
                        setTimeout(function() {
                            wx.reLaunch({
                                url: '/pages/index/index',
                            })
                        }, 1500)
                    } else {
                        wx.hideToast()
                        wx.showToast({
                            title: r.data.message,
                            icon: 'none'
                        })
                    }
                }
            })
        }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var _this = this
        console.log("index 生命周期 onload" + options)
        //在此函数中获取扫描普通链接二维码参数
        wx.showToast({
            title: 'loading...',
            icon: 'loading',
            duration: 60000
        })
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                wx.request({
                    url: app.globalData.url+'/v1/wechat/accesstoken',
                    method: 'get',
                    data: {
                        code: res.code
                    },
                    success: function(r) {
                        console.log(r)
                        if (r.data.code === 0) {
                            app.globalData.session_key = r.data.data.session_key
                            app.globalData.openId = r.data.data.openid
                            console.log(app.globalData.session_key)
                            console.log(app.globalData.openId)
                            _this.getCoupon(options)
                        } else {
                            wx.hideToast()
                            wx.showToast({
                                title: '登录失败',
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    }
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})
